# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import Mean
from ..base import OpsFeature

from .close import Close

class BBI(OpsFeature):
    """Technical Indicator - BBI

    - BBI = (MA3 + MA6 + MA12 + MA24) / 4
    """
    def __init__(self, feature=None, N1=3, N2=6, N3=12, N4=24):
        self.feature = feature or Close()
        self.N1 = N1
        self.N2 = N2
        self.N3 = N3
        self.N4 = N4

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'close':
            name += '_'+str(self.feature)
        if self.N1 != 3 or self.N2 != 6 or self.N3 != 12 or self.N4 != 24:
            name += '_{}_{}_{}_{}'.format(self.N1, self.N2, self.N3, self.N4)
        return name
    
    def _load_internal(self, instrument, start_time, end_time, freq):
        ma1 = Mean(self.feature, self.N1)
        ma2 = Mean(self.feature, self.N2)
        ma3 = Mean(self.feature, self.N3)
        ma4 = Mean(self.feature, self.N4)
        bbi = (ma1 + ma2 + ma3 + ma4) / 4
        return bbi.load(instrument, start_time, end_time, freq)
